package com.angel.controller;

import com.angel.bean.Feedback;
import com.angel.bean.Prescription;
import com.angel.bean.VipInfo;
import com.angel.service.FeedbackService;
import com.angel.service.VipInfoService;
import com.angel.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Description :
 * InitVersion : 1.0
 * CreateTime :  2018/12/16 19:30
 *
 * @author : Rabit
 * @version 1.0
 */
@Controller
@RequestMapping("/feed")
public class FeedBackController {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private VipInfoService vipInfoService;
    @Autowired
    private FeedbackService feedbackService;

    @GetMapping("/back")
    public ModelAndView userLogin(HttpSession session) {
        //反馈
        session.getAttribute("username");
        logger.info("用户反馈");
        ModelAndView mv = new ModelAndView("user_feedback");
        return mv;
    }

    /**
     * 提交反馈信息
     * @param feedback
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String addFeedBack(Feedback feedback, Model model) {
        //反馈
        feedback.setCaeatetime(new Timestamp(System.currentTimeMillis()));
        VipInfo vipInfo = new VipInfo();
        vipInfo.setUserId(feedback.getUserid());
        vipInfo = vipInfoService.findOne(vipInfo);
        if (vipInfo != null){
            feedback.setBrid(vipInfo.getBranId());
            feedback.setStatus(0);
            int i = feedbackService.insert(feedback);
            if (i ==0){
                model.addAttribute("msg","提交失败，请重试");
                return "user_feedback";
            }
        }else {
             model.addAttribute("msg","提交失败，请重试");
             return "user_feedback";
        }
        logger.info("用户反馈");
       return "redirect:" + "/c/centre";
    }


    /**
     * 获取反馈信息
     * @return
     */
    @RequestMapping("/get")
    public String getFeedBack(Model model,@RequestParam(required = false) String pageNum) {
        try {
            Integer userId= UserUtils.getPrincipal().getUserInfo().getId();
            Feedback feedback = new Feedback();
            feedback.setUserid(userId);
            int pageNo = 1;
            try {
                pageNo = Integer.valueOf(pageNum);
            } catch (Exception e) {
                pageNo =1;
            }
            PageHelper.startPage(pageNo, 8);
            List<Feedback> list = feedbackService.getAllFeedBack(feedback);

            if (list!=null){
                //有订单
                PageInfo<Feedback> page = new PageInfo<>(list);
                model.addAttribute("flag", true);
                if (page!=null){
                    model.addAttribute("pageInfo", page);
                }
            }else {
                //没有订单
                model.addAttribute("flag", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:" + "/c/centre";
        }
        logger.info("用户反馈");

        return "user_allFeedback";
    }


}
